<?php
namespace app\admin\controller;
use OSS\OssClient;
use OSS\Core\OssException;
use think\Controller;
//图片上传
class Upload extends Controller
{
    public function upload($file){
        $scr = $file->getInfo('tmp_name');
        $ext = substr($file->getInfo('name'),strrpos($file->getInfo('name'),'.')+1); // 上传文件后缀
        $dst = substr(md5($file->getRealPath()) , 0, 5).date('YmdHis').rand(0, 999999).'.'.$ext;     //上传文件名称
        $accessKeyId = config('ali_oss.AccessKeyId');
        $accessKeySecret = config('ali_oss.AccessKeySecret');
        // Endpoint以杭州为例，其它Region请按实际情况填写。
        $endpoint = config('ali_oss.endpoint');
        $bucket = config('ali_oss.bucket');

        @error_reporting (E_ALL & ~E_NOTICE & ~E_WARNING);

        //获取对象
        $auth = new OssClient($accessKeyId,$accessKeySecret,$endpoint);
        try {
            //上传图片
            $result  = $auth->uploadFile($bucket,$dst,$scr);
            return array('code'=>1,'url'=>$result['info']['url']);
        } catch (OssException $e) {
            return array('code'=>0,'msg'=>$e->getMessage());
//            return $e->getMessage();
        }
    }
}
